﻿/*
  @Description:客源详情页跟进页js
  @Author:zb,yangpl
  @Date:2014-07-22 
*/
var CustomerDetailFollow = {};
//添加提醒人
CustomerDetailFollow.appendContactsClickEvent = function (selector) {
    $.dialog({
        id: "AddRecommenddialog",
        lock: true,
        title: "添加提醒人",
        background: "#808080",
        content: '<div class="info-list">联 系 人：<input type="text" id="txtContactsName" class="ipt" title="输入姓名并从提示中选择   "><input type="hidden" id="hidContactsKeyId"><input type="hidden" id="hidDeparmentName">&nbsp;<a id="btnAddContacts" class="vm">增加</a></div><div class="info-list">联系部门：<input type="text" id="txtContactsDepartment" class="ipt" title="输入部门并从提示中选择"><input type="hidden" id="hidContactsDepartmentKeyId">&nbsp;<a id="btnAddDepartment" class="vm">增加</a></div>'
    });
    $("#btnAddContacts").bind("click", function () {
        //原来是长度小于2，不知道什么原因
        if ($('#hidContactsKeyId').val() == "") {
            $.OKDialogAgency('<div class="tc">请从智能提示中选择提醒人！</div>');
            return false;
        }
        var contactsName = $("#txtContactsName").val();
        var contactsKeyId = $("#hidContactsKeyId").val();
        var contactsDepName = $("#hidDeparmentName").val();
        var isRepeatContacts = false;
        //判断要添加的联系人是否重复
        $(selector).find("input[name=contacts]").each(function () {
            if ($(this).val() == contactsKeyId) {
                isRepeatContacts = true;
                return;
            }
        });
        if (isRepeatContacts) {
            $.OKDialogAgency('<div class="tc">不能重复添加提醒人！</div>');
            return false;
        }

        //清空联系人信息与关联隐藏域
        $("#txtContactsName").val("");
        $("#hidContactsKeyId").val("");
        var test = $(selector).html();
        test += '<span class="l">' + contactsName + '<a>' + '<i class="fclose" onclick="CustomerDetailFollow.delContactsClickEvent(this);">×</i></a><input type="hidden" name="contactsName" value="' + contactsName + '" /><input type="hidden" name="contactsKeyId" value="' + contactsKeyId + '" /><input type="hidden" name="contactsDepName" value="' + contactsDepName + '" /></span>';
        $(selector).html(test);
    });
    //新增添加部门按钮
    $("#btnAddDepartment").bind("click", function () {
        if ($('#hidContactsDepartmentKeyId').val() == "") {
            $.OKDialogAgency('<div class="tc">请从智能提示中选择提醒部门！</div>');
            return false;
        }
        var contactsDepartName = $("#txtContactsDepartment").val();
        var contactsDepartKeyId = $("#hidContactsDepartmentKeyId").val();
        var isRepeatContacts = false;
        //判断要添加的联系人是否重复
        $(selector).find("input[name=contacts]").each(function () {
            if ($(this).val() == contactsDepartKeyId) {
                isRepeatContacts = true;
                return;
            }
        });
        if (isRepeatContacts) {
            $.OKDialogAgency('<div class="tc">不能重复添加提醒人！</div>');
            return false;
        }

        //清空联系人信息与关联隐藏域
        $("#txtContactsDepartment").val("");
        $("#hidContactsDepartmentKeyId").val("");
        var test = $(selector).html();
        test += '<span class="l">' + contactsDepartName + '<a>' + '<i class="fclose" onclick="CustomerDetailFollow.delContactsClickEvent(this);">×</i></a><input type="hidden" name="contactsDepartKeyId" value="' + contactsDepartKeyId + '" /><input type="hidden" name="contactsDepartName" value="' + contactsDepartName + '" /></span>';
        $(selector).html(test);
    });

    //初始化提醒Ta的联系人查找方式
    EmployeePrompt.getDepartAndEmployeeByInput(null, null, "txtContactsName", "hidContactsKeyId", false, "private", 1, "请选择联系人", null, null, null, null, function (item) { if (item.DepartmentName) { $("#hidDeparmentName").val(item.DepartmentName); } });
    EmployeePrompt.getDepartAndEmployeeByInput("txtContactsDepartment", "hidContactsDepartmentKeyId", null, null, true, "private", 1, null, "请选择部门", null, null, null, function (item) { if (item.DepartmentName) { $("#hidDeparmentName").val(item.DepartmentName); } });

};

//删除联系人
CustomerDetailFollow.delContactsClickEvent = function (ele) {
    $(ele).parent().parent().remove();
};

//楼盘智能提示
CustomerDetailFollow.estateAutocomplete = function() {       
    $("#txtEstateBuildingName").autocomplete(takeSee.bulidingUrl, {
        width: 260,
        max: 12,
        selectFirst: false,
        formatItem: function (row) {
            return row.name;
        },
        formatResult: function (row) {
            return row.name;
        }
    });
};
CustomerDetailFollow.temparray = [];
CustomerDetailFollow.temptypecode = "";
$(function () {
    dataBind.bindOption(60, '#cmbCustomerFollowType');//客源交易类型
    //私客单选按钮
    dataBind.bindRadioByInquiryCondition(60, '#inquiryCategory1', 'foltype', 1,JSON.parse($('#divPrivilegeJsonData').html()));
    dataBind.bindRadioByInquiryCondition(60, '#inquiryCategory2', 'foltype', 2);
    //权限部分
    //如果是公客,那么判断有没有公客转私客的权限,如果没有则要消除公客转私客按纽
    if ($("#inquiryCategory2").length > 0) {
        if ($("#hidOpenInquiryToMyself").val() == "False") {
            $("#inquiryCategory2 input[itemcode=200]").parent().remove();
        }
    }
    //如果是私客,那么判断有没有转私客和转公客的权限,如果都没有,则没有转客按纽
    if ($("#inquiryCategory1").length > 0) {
        if ($("#hidTurnOtherInquiry").val() == "False" && $("#hidTurnPublicInquiry").val() == "False") {
            $("#inquiryCategory1 input[itemcode=170]").parent().remove();
        }
    }
    agencyVerify.nonnegativeInput("input[num]");
    //选择房源
    takeSee.selectPerportDiv("#takingselect", false, null);
    takeSee.selectPerportDiv("#takeselect", true, null);
    $.inputStatistics("hidcontent", 200);

    $.inputStatistics("txtContent", 200);
    $.inputStatistics("txtContentNext", 200);
    $.inputStatistics("txtPublicContentModel", 200);

    $("#writeFollow .caption label").click(function () {
        var specialInquiry = ['100','110'];
        var typevalue = $(this).find("input[name=foltype]").attr('itemcode');
        var typeGuid = $(this).find("input[name=foltype]").val();
        var extendattr = $(this).find("input[name=foltype]").attr('extendattr');
        $("#writeFollow .type-pannel").hide();
        var tempTypeCode = false;
        for (var i = 0; i < specialInquiry.length; i++) {
            if (specialInquiry[i] == typevalue) {
                tempTypeCode = true;
                break;
            }
        }
        //如果是普通跟进
        if (!tempTypeCode && CustomerDetailFollow.temptypecode!="") {
            var temflag = true;
            for (var i = 0; i < CustomerDetailFollow.temparray.length; i++) {
                if (CustomerDetailFollow.temparray[i].code == CustomerDetailFollow.temptypecode) {
                    if (extendattr == '2') {
                        CustomerDetailFollow.temparray[i].text = $("#divPublicContentModel #txtPublicContentModel").val();
                    }
                    else {
                        CustomerDetailFollow.temparray[i].text = $("#divPrivatePublicModel #txtContentModel").val();
                    }
                    temflag = false;
                    break;
                }
            }
            if (temflag) {
                if (extendattr == '2') {
                    CustomerDetailFollow.temparray.push({ code: CustomerDetailFollow.temptypecode, text:  $("#divPublicContentModel #txtPublicContentModel").val() });
                }
                else {
                    CustomerDetailFollow.temparray.push({ code: CustomerDetailFollow.temptypecode, text: $("#divPrivatePublicModel #txtContentModel").val() });
                }   
            }
        }
        if (extendattr=='2') {
            $("#divPublicContentModel").show();
            //if (typevalue == '200') {
            //    $('#txtPublicContentModel').bind('blur', function () {
            //        if ($('#txtPublicContentModel').val().length <= 0) {
            //            if ($('#spanPublicContentModel').length<=0) {
            //                $('#divPublicContentModel2').append('<span id="spanPublicContentModel" class="wrong" style=" margin-left: 520px">此项必填</span>');
            //            }                        
            //        } else {
            //            $('#spanPublicContentModel').remove();
            //        }                    
            //    });
            //} else {
            //    $('#txtPublicContentModel').unbind('blur');
            //    $('#spanPublicContentModel').remove();
            //}
            $('#btnPublicContentModel').unbind('click');
            $('#btnPublicContentModel').click(function () {
                if (typevalue=='200') {
                    takeSee.saveOtherFollow(this, typeGuid, typevalue);
                } else {
                    takeSee.saveOtherFollow(this, typeGuid);
                }
            });
            $("#divPublicContentModel #txtPublicContentModel").val("");
            for (var i = 0; i < CustomerDetailFollow.temparray.length; i++) {
                if (CustomerDetailFollow.temparray[i].code == typevalue) {
                    $("#divPublicContentModel #txtPublicContentModel").val((CustomerDetailFollow.temparray[i].text));
                    break;
                }
            }
            $.inputStatistics("txtContentModel",200);
            //逻辑结束后，把当前code存入全局变量
            CustomerDetailFollow.temptypecode = typevalue;
        } else {
            if (tempTypeCode) {
                $("#divp" + typevalue).show();
            } else {
                //私客通用模版等操作
                $('#divPrivatePublicModel').show();
                $('#btnPrivatePublicModelSubmit').unbind('click');
                $('#btnPrivatePublicModelSubmit').bind('click', function() {
                    takeSee.saveOtherFollow(this, typeGuid);
                });
                $("#divPrivatePublicModel #txtContentModel").val("");
                for (var i = 0; i < CustomerDetailFollow.temparray.length; i++) {
                    if (CustomerDetailFollow.temparray[i].code == typevalue) {
                        $("#divPrivatePublicModel #txtContentModel").val((CustomerDetailFollow.temparray[i].text));
                        break;
                    }
                }
                $.inputStatistics("txtContentModel",200);
                //逻辑结束后，把当前code存入全局变量
                CustomerDetailFollow.temptypecode = typevalue;
            }
        }
       
        $(".wrong").hide();
    });
    //将客源动态放在最左边
    $('input[itemcode="160"]').parent().addClass('l');
    //将客源动态默认选中
    $("#writeFollow .caption #inquiryCategory1 label").eq(2).find("input").attr("checked", "checked").click();
    //公客默认选中电话无效
    $("#writeFollow .caption #inquiryCategory2 label").eq(0).find("input").attr("checked", "checked").click();
    //带看类型改变事件
    $("#cmbSeePropertyType").unbind("change").bind("change", function () {
        if ($(this).val() == "20") {
            $("#salePriceSpan").show();
            $("#rentPriceSpan").hide();
        } else if ($(this).val() == "10") {
            $("#salePriceSpan").hide();
            $("#rentPriceSpan").show();
        }
    });

    //陪看人智能提示
    EmployeePrompt.getEmployeeByInput('txtEmployeeName', 'hidUserId', true, 'private');

    //上传看房单
    var settings = {
        flash_url: "/Scripts/PageHelper/swfupload.swf",
        upload_url: $("#uploadPath").val(),
        post_params: {},
        file_size_limit: "2 MB",
        file_types: "*.jpg;*.gif;*.png",
        file_types_description: "JPG Images",
        file_upload_limit: 20,
        file_queue_limit: 20,
        custom_settings: {
            progressTarget: "fsUploadProgress"
        },
        debug: false,
        // Button settings
        button_image_url: "",
        button_width: "80",
        button_height: "20",
        button_placeholder_id: "uploadTakeSeeList",
        button_cursor: SWFUpload.CURSOR.HAND,
        button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
        button_text: '<span class="btntext">上传看房单</span>',
        button_text_style: ".btntext{color:#1f73f0;}",
        button_text_left_padding: 12,
        button_text_top_padding: 3,

        // The event handler functions are defined in handlers.js
        file_queued_handler: fileQueued,
        file_queue_error_handler: fileQueueError,
        file_dialog_complete_handler: fileDialogComplete,
        upload_start_handler: function () { $(".swfload").show(); },
        upload_progress_handler: function () { $(".swfload").show(); },
        upload_error_handler: uploadError,
        upload_success_handler: uploadSuccessTakeSeeList,
        upload_complete_handler: function () { $(".swfload").hide(); },
        queue_complete_handler: queueComplete	// Queue plugin event


    };
    var swfu = new SWFUpload(settings);
    //权限部分
});
